//test.c
#include "Binary_conversion_helper.h"
#include "Heap_memory_management_helper.h"
#include "Binary_tree.h"

void test_1();
void test_2();
void test_3();
void test_4();
void test_5();
void test_6();
void test_7();

//获取整数的原反补,正数原反补相同
void test_1(){
    const char *str1,*str2,*str3;
    int num;
    printf("var : ");
    scanf("%d",&num);
    str1 = D_to_B_original(num);
    str2 = D_to_B_inverse(num);
    str3 = D_to_B_complement(num);
    printf("B_original:%s \nB_inverse:%s \nB_complement:%s \n",str1,str2,str3);
}



//IEEE浮点表示
void test_2(){
    PRINT_BINARY_DOUBLE(-0.12345);
    PRINT_BINARY_FLOAT(-0.845);
    double ans1 = double_IEEE_conversion_to_D("1       0111 1111 011   1111 1001 1010 0110 1011 0101 0000 1011 0000 1111 0010 0111 1100");
    float ans2 = float_IEEE_conversion_to_D("1       0111 1110       1011 0000 1010 0011 1101 100");
    float NaN = float_IEEE_conversion_to_D("0       1111 1111       0000 0000 0000 0000 0000 000");
    float infinity = float_IEEE_conversion_to_D("0       1111 1111       1000 0000 0000 0000 000");
    printf("%f \n %f \n NaN:%f \n infinity:%f \n",ans1 ,ans2,NaN,infinity);
}


//测试矩阵管理器
void test_3(){
    Manager* manager = manager_instance();

    // 创建矩阵 A
    Matrix* A = create_matrix(2, 3);
    const char* A_str = "1 2 3\n4 5 6";
    set_matrix(A, A_str);

    // 创建矩阵 B
    Matrix* B = create_matrix(3, 2);
    const char* B_str = "7 8\n9 10\n11 12";
    set_matrix(B, B_str);

    // 计算矩阵乘积
    Matrix* C = matrix_multiply(A, B);

    // 显示结果矩阵
    printf("Matrix A:\n");
    show_matrix(A);
    printf("Matrix B:\n");
    show_matrix(B);
    printf("Matrix C (A * B):\n");
    show_matrix(C);

    // 销毁管理器
    destruct_manager();
    //system("pause");

}


//矩阵的运算
void test_4(){
    Matrix* A = create_matrix(3, 3);
    // int arr[3][3] = {
    // {4, 1, -2},
    // {1, 2, -1},
    // {-2, -1, 3}
    // };
    set_matrix(A, "4, 1, -2\n1, 2, -1\n-2, -1, 3");
    //copy_array_to_matrix(A, 3, 3, arr);

    printf("Original Matrix:\n");
    show_matrix(A);

    double ans = determinant(A);
    Matrix_double* inverse = matrix_inverse(A);
    printf("The value of the determinant: %.2f \n",ans);
    printf("Inverse Matrix:\n");
    show_double_matrix(inverse);


    // 计算特征值和特征向量
    double eigenvalues[1];
    double eigenvectors[A->rows];

    matrix_eigen(A, eigenvalues, eigenvectors);
    printf("Eigenvalue: %f\n", eigenvalues[0]);
    printf("Eigenvector: ");
    for (int i = 0; i < A->rows; i++) {
        printf("%f ", eigenvectors[i]);
    }
    printf("\n");

    Manager *manager = manager_instance();
    printf("\nmanager number: %d \n",manager->all_num);
    destruct_manager();
}

//对二叉树的节点对象测试特性
void test_5(){
    Node *root = (Node *)malloc(sizeof(Node));
    Node *node_1 = (Node *)malloc(sizeof(Node));
    Node *node_2 = (Node *)malloc(sizeof(Node));
    root->type = N_INTERNAL;

    root->info.internal.left = node_1;
    root->info.internal.right = node_2;
    printf("NULL:%p  %p  %p \n",NULL,root->info.internal.left,root->info.internal.right);

    root->info.data[0] = 1;
    root->info.data[1] = 2;

    printf("Binary tree: %f %f \n",root->info.data[0],root->info.data[1]);
    printf("%p  %p \n",root->info.data[0],root->info.data[1]);
    printf("%p  %p \n",root->info.internal.left,root->info.internal.right);

}

//malloc等分配内存函数(强制内存对齐的后果)
void test_6(){
    /*当调用 malloc(n) 时，实际分配的内存大小可能大于 n，因为：
    元数据头：通常占用 8~16 字节（具体取决于实现）。
    对齐填充：为了满足对齐要求，内存块大小会被向上舍入到对齐值的倍数（例如 16、32、48 字节等）。*/
    int *ptr_1 = (int *)malloc(6*sizeof(int));
    int *ptr_2 = (int *)malloc(7*sizeof(int));
    char *ptr_3 = (char *)malloc(sizeof(char));
    double *ptr_4 = (double *)malloc(sizeof(double));

    printf("%p %lld\n",ptr_1,(unsigned long long)ptr_1/16);
    printf("%p %lld\n",ptr_2,(unsigned long long)ptr_2/16);
    printf("%p %lld\n",ptr_3,(unsigned long long)ptr_3/16);
    printf("%p %lld\n",ptr_4,(unsigned long long)ptr_4/16);
    printf("ptr_2 and ptr_1 Difference value : %lld Byte \n",((unsigned long long)ptr_2-(unsigned long long)ptr_1));
    printf("ptr_3 and ptr_2 Difference value : %lld Byte \n",((unsigned long long)ptr_3-(unsigned long long)ptr_2));
    printf("ptr_4 and ptr_3 Difference value : %lld Byte \n",((unsigned long long)ptr_4-(unsigned long long)ptr_3));

    free(ptr_1);
    free(ptr_2);
    free(ptr_3);
    free(ptr_4);
}

